package org.jetlinks.community.rule.engine.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jetlinks.community.rule.engine.entity.DvAlarm;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class HttpRestTemplate {

    //物联网平台登录 获取token
    public static String login(String url) {

        Map<String, Object> params = new HashMap<>();
        params.put("username", "admin");
        params.put("password", "admin@123");
        JSONObject jsonObject = sendPost(url, params, "application/json", null);
        String token = (String) jsonObject.get("token");
        return token;
    }


    /**
     * 发送post请求
     *
     * @param url       地址
     * @param parameter 参数
     * @return
     */
    public static JSONObject sendPost(String url, DvAlarm parameter, String type, String token) {
        RestTemplate client = new RestTemplate();
        client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(type));
        if (StringUtils.isNotEmpty(token)) {
            headers.set("Authorization", token);
        }
        HttpMethod method = HttpMethod.POST;
        // 将请求头部和参数合成一个请求
        HttpEntity<DvAlarm> requestEntity = new HttpEntity<>(parameter, headers);
        // 执行HTTP请求，将返回的结构使用JSONObject类格式化
        ResponseEntity<String> result = client.exchange(url, method, requestEntity, String.class);
        return JSONObject.parseObject(result.getBody());
    }

    /**
     * 发送post请求
     *
     * @param url       地址
     * @param parameter 参数
     * @return
     */
    public static JSONObject sendPost(String url, Map<String,Object> parameter, String type, String token) {
        RestTemplate client = new RestTemplate();
        client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(type));
        if (StringUtils.isNotEmpty(token)) {
            headers.set("X-Access-Token", token);
        }
        HttpMethod method = HttpMethod.POST;
        // 将请求头部和参数合成一个请求
        HttpEntity<Map<String,Object>> requestEntity = new HttpEntity<>(parameter, headers);
        // 执行HTTP请求，将返回的结构使用JSONObject类格式化
        ResponseEntity<String> result = client.exchange(url, method, requestEntity, String.class);
        return JSONObject.parseObject(result.getBody());
    }

    /**
     * 发送post请求,List形参
     *
     * @param url       地址
     * @param parameter 参数
     * @return
     */
    public static JSONObject sendPost(String url, List<Map<String, Object>> parameter, String type, String token) {
        RestTemplate client = new RestTemplate();
        client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(type));
        if (StringUtils.isNotEmpty(token)) {
            headers.set("X-Access-Token", token);
        }
        HttpMethod method = HttpMethod.POST;
        // 将请求头部和参数合成一个请求
        HttpEntity<List<Map<String, Object>>> requestEntity = new HttpEntity<>(parameter, headers);
        // 执行HTTP请求，将返回的结构使用JSONObject类格式化
        ResponseEntity<String> result = client.exchange(url, method, requestEntity, String.class);
        return JSONObject.parseObject(result.getBody());
    }


    /**
     * 发送post请求
     * @param url 地址
     * @return
     */
    public static JSONObject sendGet(String url) {
        RestTemplate client = new RestTemplate();
        // 执行HTTP请求，将返回的结构使用JSONObject类格式化
        ResponseEntity<String> result = client.getForEntity(url, String.class);
        return JSONObject.parseObject(result.getBody());
    }


}
